import QtQuick
import QtQuick.Layouts
import QtQuick.Controls as Controls
import org.kde.kirigami as Kirigami
import org.kde.kirigamiaddons.formcard as FormCard
import Qt.labs.platform as Lab

Kirigami.ApplicationWindow {

    //全局参数
    property string baselineFile: "none"
    property string dataPath: "none"
    property string backend: "backend" //成像后端可执行文件名，这里没有添加可调整选型

    //END全局参数
    id: root
    width: 250
    height: 750

    title: i18nc("@title:window", "1")

    //pageStack.initialPage:imagingSetting
    Controls.SwipeView {
        id: pageview
        objectName: "pageview"
        anchors.fill: parent

        //成像设置页
        FormCard.FormCardPage {
            id: imagingSetting
            //objectName: "imagingSetting"

            //Header
            header: Controls.ToolBar {
                RowLayout {
                    Controls.Button {
                        id: start
                        objectName: "start"
                        signal signalstart
                        visible: which_source.currentIndex === 0
                        icon.name: "builder-run-start-symbolic"
                        text: i18n("开始成像")
                        onClicked: {
                            save.enabled = true
                            stop.enabled = true
                            start.enabled = false
                            signalstart()
                        }
                        //Connections:{

                        //}
                    }
                    Controls.Button {
                        id: save
                        objectName: "save"
                        signal signalsave
                        enabled: false
                        visible: which_source.currentIndex === 0
                        icon.name: "builder-run-start-symbolic"
                        text: i18n("保存数据")
                        onClicked: {
                            save.enabled = false
                            stop.enabled = true
                            start.enabled = false
                            signalsave()
                        }
                    }
                    Controls.Button {
                        id: stop
                        objectName: "stop"
                        signal signalstop
                        enabled: false
                        visible: which_source.currentIndex === 0
                        icon.name: "builder-run-start-symbolic"
                        text: i18n("停止")
                        onClicked: {
                            save.enabled = false
                            stop.enabled = false
                            start.enabled = true
                            signalstop()
                        }
                    }
                    Controls.Button {
                        id: output
                        visible: which_source.currentIndex === 1
                        icon.name: "filesave"
                        text: i18n("导出为视频")
                        //onClicked:
                    }
                    // …可继续添加更多按钮或组件…
                }
            }

            //END Header
            FormCard.FormHeader {
                title: i18n("基本设置")
            }
            FormCard.FormCard {
                FormCard.FormComboBoxDelegate {
                    id: method
                    objectName: "method"
                    text: i18n("方法")
                    displayMode: FormCard.FormComboBoxDelegate.ComboBox
                    currentIndex: 0
                    editable: false
                    model: ["绝对成像", "差分成像"]
                }

                FormCard.FormButtonDelegate {
                    id: baseline
                    visible: method.currentIndex === 1
                    text: "基线数据："
                    onClicked: {
                        fileDialog.open()
                    }
                }

                //算法选择框
                FormCard.FormComboBoxDelegate {
                    id: algorithm_dynamic
                    objectName: "algorithm_dynamic"
                    visible: method.currentIndex === 1
                    text: i18n("算法")
                    //description: i18n("选择")
                    displayMode: FormCard.FormComboBoxDelegate.ComboBox
                    currentIndex: 0
                    editable: false
                    model: ["bp", "jac", "svd", "greit"]
                }
                FormCard.FormComboBoxDelegate {
                    id: algorithm_static
                    objectName: "algorithm_static"
                    visible: method.currentIndex === 0
                    text: i18n("算法")
                    //description: i18n("选择")
                    displayMode: FormCard.FormComboBoxDelegate.ComboBox
                    currentIndex: 0
                    editable: false
                    model: ["jac"]
                }

                //END算法选择框
                FormCard.FormComboBoxDelegate {
                    id: n_el
                    objectName: "n_el"
                    text: i18n("电极数")
                    displayMode: FormCard.FormComboBoxDelegate.ComboBox
                    currentIndex: 0
                    editable: false
                    model: [8, 16, 32, 64]
                }

                //数据源选择
                FormCard.FormComboBoxDelegate {
                    id: which_source
                    objectName: "which_source"
                    text: i18n("数据源")
                    displayMode: FormCard.FormComboBoxDelegate.ComboBox
                    //description: i18n("非本机数据需转发至TCP")
                    currentIndex: 0
                    editable: false
                    model: ["远程", "本地"]
                }
                FormCard.FormTextFieldDelegate {
                    id: data_url
                    objectName: "data_url"
                    visible: which_source.currentIndex === 0
                    label: i18n("链接")
                    text: "127.0.0.1:8100"
                    //...
                }
                FormCard.FormButtonDelegate {
                    id: data_path
                    visible: which_source.currentIndex === 1
                    text: i18n("文件夹：")
                    onClicked: {
                        folderDialog.open()
                    }
                }//没做界面的载入设置，无所谓了
            }

            //END数据源选择
            FormCard.FormHeader {
                title: i18n("高级设置")
            }
            FormCard.FormCard {
                FormCard.FormTextFieldDelegate {
                    id: dist_exc
                    objectName: "dist_exc"
                    label: i18n("激励间距")
                    validator: IntValidator {
                        bottom: 1
                        top: n_el.currentText / 2
                    }
                    text: "1"
                    onTextChanged: {
                        if (text === "")
                            return
                        var v = parseInt(dist_exc.text)
                        if (v > (n_el.currentText) / 2)
                            v = n_el.currentText / 2
                        dist_exc.text = v.toString()
                    }
                }
                FormCard.FormTextFieldDelegate {
                    id: step_meas
                    objectName: "step_meas"
                    label: i18n("测量步长")
                    validator: IntValidator {
                        bottom: 1
                        top: n_el.currentText / 2
                    }
                    text: "1"
                    onTextChanged: {
                        if (text === "")
                            return
                        var v = parseInt(step_meas.text)
                        if (v > (n_el.currentText) / 2)
                            v = n_el.currentText / 2
                        step_meas.text = v.toString()
                    }
                }
                FormCard.FormTextFieldDelegate {
                    id: mesh_size
                    objectName: "mesh_size"
                    label: i18n("单元尺寸")
                    text: "0.1"
                }

                FormCard.FormComboBoxDelegate {
                    id: start_el
                    objectName: "start_el"
                    text: i18n("电压测量编号起始位置")
                    displayMode: FormCard.FormComboBoxDelegate.ComboBox
                    //description: i18n("std:电压测量从第一个电极开始编号|fmmu:电压测量从电流源电极A开始编号")
                    currentIndex: 0
                    editable: false
                    model: ["第一个电极", "电流源流入电极"]
                }
            }
            FormCard.FormHeader {
                title: i18n("算法设置")
            }
            FormCard.FormCard {
                FormCard.FormTextFieldDelegate {
                    visible: algorithm_static.currentText === "jac"
                             && algorithm_static.visible === true
                    id: static_jac_setting
                    label: "test_jac_static"
                }
            }
            FormCard.FormTextFieldDelegate {
                id: dynamic_jac_setting
                visible: algorithm_dynamic.currentText === "jac"
                         && algorithm_dynamic.visible === true
                label: "test_jac_dynamic"
            }

            //####################################
            Lab.FileDialog {
                id: fileDialog
                title: i18n("选择基线数据文件")
                folder: shortcuts.home // 默认打开到用户主目录
                nameFilters: [i18n("所有文件 (*)")]
                onAccepted: {
                    baselineFile = fileDialog.file
                    baseline.text = "基线数据：" + baselineFile
                }
                onRejected: {

                    //console.log("文件选择已取消")
                }
            }
            Lab.FolderDialog {
                id: folderDialog
                title: i18n("选择文件夹")
                folder: shortcuts.home // 默认打开到用户主目录
                onAccepted: {
                    dataPath = folderDialog.folder
                    data_path.text = "文件夹：" + dataPath
                }
                onRejected: {

                    //console.log("文件选择已取消")
                }
            }
        }

        //END成像设置页
        FormCard.FormCardPage {
            id: dispatchSetting
            //Header
            header: Controls.ToolBar {
                RowLayout {
                    Controls.Button {
                        id: nomean
                        visible: which_source.currentIndex === 0
                        icon.name: "builder-run-start-symbolic"
                        text: i18n("无意义")
                        onClicked: {

                        }
                    }
                }
            }

            //END Header
            FormCard.FormHeader {
                title: i18n("算法设置")
            }
        }
    }
    //dock
    footer: Kirigami.NavigationTabBar {
        //objectName: "navigation"
        actions: [
            Kirigami.Action {
                icon.name: "globe"
                text: i18n("成像设置")
                //checked: imagingSetting.visible
                onTriggered: {
                    pageview.currentIndex = 0
                }
            },
            Kirigami.Action {
                icon.name: "player-time"
                text: i18n("参数下发")
                //checked: dispatchSetting.visible
                onTriggered: {
                    pageview.currentIndex = 1
                }
            }
        ]
    }
    //END dock
}
